import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart';

/// 获取验证码请求模型
class SmsCodeReq {
  /// 是否国际号码 (1:是 0:否)
  final int isInternational;
  
  /// MD5加密的key
  final String key;
  
  /// 短信类型 (1:注册 2:找回密码 3:登录 4:绑定手机号 5:解绑手机号)
  final int smsType;
  
  /// 手机号
  final String tel;
  
  /// 时间戳 (格式: "yyyy-MM-dd HH:mm:ss")
  final String tm;
  
  SmsCodeReq({
    this.isInternational = 1,
    required this.key,
    required this.smsType,
    required this.tel,
    required this.tm,
  });
  
  /// 创建登录验证码请求
  static SmsCodeReq forLogin(String mobile) {
    final now = DateTime.now();
    final tm = '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} '
        '${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}';
    
    // 生成key: MD5(手机号_jRnQh8ed_时间戳)
    final keyStr = '${mobile}_jRnQh8ed_$tm';
    final key = md5.convert(utf8.encode(keyStr)).toString();
    
    debugPrint('📱 生成验证码请求:');
    debugPrint('  - 手机号: $mobile');
    debugPrint('  - 时间戳: $tm');
    debugPrint('  - Key字符串: $keyStr');
    debugPrint('  - MD5 Key: $key');
    
    return SmsCodeReq(
      isInternational: 1,
      key: key,
      smsType: 3, // 登录
      tel: mobile,
      tm: tm,
    );
  }
  
  Map<String, dynamic> toJson() {
    return {
      'isInternational': isInternational,
      'key': key,
      'smsType': smsType,
      'tel': tel,
      'tm': tm,
    };
  }
  
  @override
  String toString() {
    return 'SmsCodeReq(tel: $tel, smsType: $smsType, tm: $tm)';
  }
}

/// 验证码登录请求模型
class PhoneCodeLoginReq {
  /// App版本名称
  final String appVersion;
  
  /// App版本号
  final String appVersionCode;
  
  /// 验证码
  final String code;
  
  /// 手机号
  final String mobile;
  
  /// 手机厂商
  final String phoneFirm;
  
  /// 手机型号
  final String phoneModel;
  
  /// 设备唯一标识
  final String phoneUuid;
  
  /// 手机号区域 (默认86-中国)
  final String smsRegion;
  
  PhoneCodeLoginReq({
    required this.appVersion,
    required this.appVersionCode,
    required this.code,
    required this.mobile,
    required this.phoneFirm,
    required this.phoneModel,
    required this.phoneUuid,
    this.smsRegion = '86',
  });
  
  Map<String, dynamic> toJson() {
    return {
      'appVersion': appVersion,
      'appVersionCode': appVersionCode,
      'code': code,
      'mobile': mobile,
      'phoneFirm': phoneFirm,
      'phoneModel': phoneModel,
      'phoneUuid': phoneUuid,
      'smsRegion': smsRegion,
    };
  }
  
  @override
  String toString() {
    return 'PhoneCodeLoginReq(mobile: $mobile, code: $code)';
  }
}

